<template>
	<button v-if="!isuser" class="pug-button-userinfo" open-type="getUserInfo" @getuserinfo="handleGetUserInfo"></button>
</template>


<script>
	import cache from '@/config/cache'
	import userService from '@/service/user/UserService'
	export default {
		name:"pug-userinfo",
		data() {
			return {
				isuser:false
			};
		},
		created(){
			this.isuser = cache.get("isuser");
		},
		methods:{
			async handleGetUserInfo(e){
				 try{
				 	//数据库的同步和更新即可
				 	var userinfo = e.detail.userInfo;
				 	var userBo = await userService.updateUserweixin(userinfo);
					// 获取缓存用户信息进行替换
					var cacheUserInfo = getApp().getUser();
					cacheUserInfo.avatar = userinfo.avatarUrl;
					cacheUserInfo.nickname = userinfo.nickName;
					cacheUserInfo.country = userinfo.country;
					cacheUserInfo.province = userinfo.province;
					cacheUserInfo.city = userinfo.city;
					cacheUserInfo.sex = userinfo.gender;
					// 同步缓存 	
					getApp().setUser(cacheUserInfo);
					// 当前授权弹出关闭，并且全局范围的开关全部关闭
					this.isuser = true;
					cache.set("isuser",true);
					uni.$u.toast("授权成功");
					
				 }catch(e){
					 
				 }
			},
			
			handleGetPhoneNumber(e){
				console.log("获取用户的手机号码是信息是：",e)
			}
		}
	}
</script>

<style>
	.pug-button-userinfo{position: fixed;top:0;left:0;right:0;bottom:0;background:transparent;z-index: 10000;}
</style>
